﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;
using System.Collections.Specialized;
using System.Text.RegularExpressions;

public partial class Whir_System_Module_Information_ContentManage : System.Web.UI.Page
{
    protected int TypeID, ModelID, ModelFormID;
    protected string Where;
    protected string key;

    /// <summary>
    /// 获取当前功能模块对应的表的名称
    /// </summary>
    protected string tableName = "";

    /// <summary>
    /// 获取当前功能模块对应表的主键名称
    /// </summary>
    protected string primaryKeyName = "";

    /// <summary>
    /// 跳转页面的标题
    /// </summary>
    protected string PopWindowTitle = "";
    /// <summary>
    /// 添加按钮跳转的Url
    /// </summary>
    protected string AddUrl = "";


    public GridView GridView
    {
        get { return this.NewCommonContentManage1.GridView; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        key = Whir.Common.Settings.GetString("AdminCacheKey");
        ModelID = NetRequest.GetQueryInt("ModelID");
        ModelFormID = NetRequest.GetQueryInt("ModelFormID");
        TypeID = NetRequest.GetQueryInt("TypeID");

        Whir.Model.Whir_Model model = new Whir.Model.Whir_Model();
        Whir.BLL.Whir_Model bll = new Whir.BLL.Whir_Model();
        model = bll.GetModel(ModelID);
        tableName = model.tableName;
        primaryKeyName = ContentManage.GetCustomTablePrimaryKeyName(model.tableName);
        AddUrl = "Content.aspx?Action=Add&ModelID=" + ModelID + "&ModelFormID=" + ModelFormID + "&TypeID=" + TypeID + "&url=" + Server.UrlEncode(NetRequest.GetUrl());

        this.NewCommonContentManage1.CommonContentManage_RowCommand += new UserControls_ContentControl_NewCommonContentManage.RowCommand(NewCommonContentManage1_CommonContentManage_RowCommand);

        if (!IsPostBack)
        {
            if (TypeID != 0)
            {
                //初始化搜索条件
                ContentSearch1.ModelFormID = ModelFormID;
                ContentSearch1.TypeID = TypeID;

                //初始化内容管理条件
                Where = " TypeID =" + TypeID;
                NewCommonContentManage1.ModelID = ModelID;
                NewCommonContentManage1.ModelFormID = ModelFormID;
                NewCommonContentManage1.TypeID = TypeID;
                NewCommonContentManage1.Where = Where;
                //为列表添加动作,如果有多个动作刚用"|"分开
                //参数说明: [事件|链接],[show|hide],[列头],[内容],[命令名称|链接地址]
                NewCommonContentManage1.Settings = ""; //"1,show,应聘信息,查看,ShowItem";

                Whir.BLL.Whir_Column Whir_Column_bll = new Whir.BLL.Whir_Column();
                Whir.Model.Whir_Column Whir_Column_model = Whir_Column_bll.GetModel(TypeID);
                lbColumnName.Text = Whir_Column_model.columnName;


                //初始化同级别的所有栏目
                BindDrop();
            }
        }
    }

    void NewCommonContentManage1_CommonContentManage_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //int rowID = Convert.ToInt32(e.CommandArgument);
        //GridViewRow selectedRow = this.CommonContentManage1.GridView.Rows[rowID];
        //string itemID = this.CommonContentManage1.GridView.DataKeys[rowID].Value.ToString();
        //if (e.CommandName == "ShowItem")
        //{
        //    Response.Redirect("JobRequestList.aspx?" + "&TypeID=" + TypeID + "&ItemID=" + itemID + "&url=" + Server.UrlEncode(NetRequest.GetUrl()));
        //}
    }


    private void BindDrop()
    {
        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        Whir.Model.Whir_Column model = bll.GetModel(TypeID);

        if (model != null)
        {
            string sql = string.Format("SELECT columnID, columnName FROM Whir_Column WHERE parentID = {0}", model.parentID);
            DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(sql).Tables[0];

            dropCategroy.DataTextField = "columnName";
            dropCategroy.DataValueField = "columnID";
            dropCategroy.DataSource = dt;
            dropCategroy.DataBind();

            dropCategroy.SelectedValue = TypeID.ToString();
        }
    }


    #region 头部导航按钮事件
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "退出";
        model.operateType = 6;      //退出
        model.operateResult = true;
        model.operateDesc = "退出";

        Whir.Log.OperateLog.Add(model);

        HttpContext.Current.Response.Cookies.Remove(key);

        Response.Redirect("~/Whir_system/login.aspx");
        return;
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Whir.Log.Model.Whir_OperateLog model = new Whir.Log.Model.Whir_OperateLog();
        model.title = "退出";
        model.operateType = 6;      //退出
        model.operateResult = true;
        model.operateDesc = "退出";

        Whir.Log.OperateLog.Add(model);

        HttpContext.Current.Response.Cookies.Remove(key);

        Response.Redirect("~/Whir_system/login.aspx");
        return;
    }
    #endregion


    #region 操作按钮事件

    #region 批量删除到回收站
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        //获取被选中的编号
        int[] itemIds = this.NewCommonContentManage1.ezGridView.CheckedIndexs;
        if (itemIds.Length > 0)
        {
            string execsql = "";
            object obj;
            string file = "";
            foreach (int item in itemIds)
            {
                execsql = string.Format("DELETE FROM {0} WHERE {1} = {2}", tableName, primaryKeyName, item);

                Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(execsql);

                obj = Whir.SQLServerDAL.DbHelperSQL.GetSingle(string.Format("SELECT Top 1 [CreateDate] From {0} WHERE {1} = {2}", tableName, primaryKeyName, item));
                if (obj != null)
                {
                    file = string.Format("~/html/{0}/{1}.html", ContentManage.GetDateTimeFormat(obj), TypeID + "_" + item);
                    file = Server.MapPath(file);
                    if (System.IO.File.Exists(file))
                    {
                        System.IO.File.Delete(file);
                    }
                }
            }
            JsHelper.Alert(Page, "该记录已删除成功！");

            ////执行发布操作
            //string[] msgstr = ContentManage.publish(TypeID, Page);
            //bool flag = bool.Parse(msgstr[0]);
            //if (!flag)
            //{
            //    JsHelper.Alert(Page, "发布出错！" + msgstr[1]);
            //}
        }
        else
        {
            JsHelper.Alert(Page, "请选择要删除的记录！");
            return;
        }

        this.NewCommonContentManage1.BindData();

        //BindData();
    }
    #endregion
    #endregion


    //yhe 2009-05-09
    //新增加添加类别的方法
    /// <summary>
    /// 添加类别
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAddColumnServer_Click(object sender, EventArgs e)
    {
        AddColumn();
    }

    /// <summary>
    /// 添加栏目的方法
    /// </summary>
    private void AddColumn()
    {
        if (TypeID != 0)
        {
            //查找当前栏目并创建其副本
            Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
            Whir.Model.Whir_Column model = bll.GetModel(TypeID);

            if (model != null)
            {
                model.columnMark = "Whir_" + DateTime.Now.ToString();   //暂时无用
                model.columnName = txtColumnName.Text.Trim();           //栏目名称
                model.url = txtUrl.Text.Trim();                         //页面名称
                model.sort = int.Parse(txtSort.Text);                   //排序
                model.createDate = DateTime.Now;                        //创建时间
                model.updateDate = DateTime.Now;                        //更新时间

                bll.Add(model);

                if (model.isUse)
                {
                    RoleHelper.AddDefaultRole();
                    RoleHelper.AddCacheRole();
                }
            }

            //关闭层
            //刷新左则菜单导航
            ClientScript.RegisterStartupScript(typeof(Page), "key", "openBg(0);openSelect(0);ReloadTree();", true);
        }
    }


    //yhe 2009-05-09
    //添加类别转移的方法
    protected void btnChange_Click(object sender, EventArgs e)
    {
        //获取被选中的编号
        int[] itemIds = this.NewCommonContentManage1.ezGridView.CheckedIndexs;
        if (itemIds.Length > 0)
        {
            foreach (int item in itemIds)
            {
                //修改当前选中行的TypeID值
                string execsql = string.Format(
                    "UPDATE {0} SET TypeID = {1} WHERE {2} = {3}",
                    tableName, dropCategroy.SelectedValue, primaryKeyName, item);
                Whir.SQLServerDAL.DbHelperSQL.ExecuteSql(execsql);
            }

            JsHelper.Alert(Page, "记录修改成功！");

            this.NewCommonContentManage1.BindData();
        }
        else
        {
            JsHelper.Alert(Page, "请选择要修改的记录！");
            return;
        }
    }
}
